package com.kbbywyclient.ui.activity;
import android.content.Intent;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.BankCardInfo;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.CardHolderInfoModule;
import com.kbbywyclient.presenter.CardHolderInfoPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.CardHolderInfoContract;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.IdCardValidator;
import com.kbbywyclient.util.ToastUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.widget.CustomDialog;
import com.kbbywyclient.widget.CustomToolBar;

import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Jiangzx on 2017/9/8 9:59.
 * 持卡人信息
 */

public class CardHolderInfoActivity extends BaseActivity implements CardHolderInfoContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.et_cardholder)
    EditText etCardHolder;//持卡人
    @BindView(R.id.et_card_number)
    EditText etCardNumber;//银行卡号码
    @BindView(R.id.et_id_card_number)
    EditText etIdCardNumber;//身份证号码

    @Inject
    CardHolderInfoPresenter presenter;

    @BindView(R.id.bt_next_stop)
    Button btNextStep;//下一步按钮

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cardholder_info;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->handleGoBack());
    }

    @Override
    public void initData() {
        Intent intent=getIntent();
        String cardHolder=intent.getStringExtra(VerifyPhoneActivity.NAME_S);
        ViewUtils.setText(etCardHolder,cardHolder);
        String cardNumber=intent.getStringExtra(VerifyPhoneActivity.CARD_NUMBER_S);
        ViewUtils.setText(etCardNumber,cardNumber);
        String idCardNumber=intent.getStringExtra(VerifyPhoneActivity.ID_CARD_NUMBER_S);
        ViewUtils.setText(etIdCardNumber,idCardNumber);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("银行卡绑定-持卡人信息");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getCardHolderInfoComponent(new CardHolderInfoModule(this)).inject(this);
    }

    CustomDialog cancelDialog;
    private void handleGoBack(){
        if(cancelDialog==null){
            cancelDialog=new CustomDialog(CardHolderInfoActivity.this,
                    R.style.Customdialog, "卡贝贝提示", "是否取消绑卡？", new CustomDialog.ButtonListener() {
                @Override
                public void left() {
                    cancelDialog.dismiss();
                    finish();
                }

                @Override
                public void right() {
                    cancelDialog.dismiss();
                }
            },"是","否");
        }
        if(!cancelDialog.isShowing()){
            cancelDialog.show();
        }
    }

    @OnTextChanged({R.id.et_cardholder,R.id.et_card_number,R.id.et_id_card_number})
    public void afterTextChanged(Editable s){
        String name=etCardHolder.getText().toString().trim();
        String card=etCardNumber.getText().toString().trim();
        String idCard=etIdCardNumber.getText().toString().trim();
        if(name.length()>0&&card.length()>0&&idCard.length()>0){
            btNextStep.setEnabled(true);
        }else{
            btNextStep.setEnabled(false);
        }
    }

    /**
     * 检查持卡人姓名
     * @return
     */
    private boolean checkName(){
        boolean result=false;
        String name=etCardHolder.getText().toString().trim();
        if(name.length()==0){
            ToastUtil.showShort("请输入持卡人姓名");
        }else {
            if(name.length()<2){
                ToastUtil.showShort("请输入正确的持卡人姓名");
            }else{
                result=true;
            }
        }
        return result;
    }

    /**
     * 检查银行卡号
     * @return
     */
    private boolean checkCardNumber(){
        boolean result=false;
        String cardNumber=etCardNumber.getText().toString().trim();
        if(cardNumber.length()==0){
            ToastUtil.showShort("请输入银行卡卡号");
        }else{
            result=true;
        }
        return result;
    }

    /**
     * 检查身份证
     * @return
     */
    private boolean checkIdCard(){
        boolean result=false;
        String idCard=etIdCardNumber.getText().toString().trim();
        if(idCard.length()==0){
            ToastUtil.showShort("请输入身份证号码");
        }else{
            if(idCard.length()<18){
                ToastUtil.showShort("您输入的身份证号位数有误，请重新输入");
            }else{
                IdCardValidator validator=new IdCardValidator();
                if(validator.isValidate18Idcard(idCard)){
                    result=true;
                }else{
                    ToastUtil.showShort("请输入正确的身份证号码");
                }
            }
        }
        return result;
    }


    @OnClick(R.id.bt_next_stop)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_next_stop:
                if(checkName()&&checkCardNumber()&&checkIdCard()){
                    presenter.getBankCardInfo(etCardNumber.getText().toString().trim());
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            handleGoBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void loadBankCardInfoSuccess(BankCardInfo cardInfo) {
        if(cardInfo.getCard_type()==1){
            //储蓄卡
            HashMap<String,Object> params=new HashMap<>();
            params.put(VerifyPhoneActivity.NAME_S,
                    etCardHolder.getText().toString().trim());
            params.put(VerifyPhoneActivity.ID_CARD_NUMBER_S,
                    etIdCardNumber.getText().toString().trim());
            params.put(VerifyPhoneActivity.CARD_NUMBER_S,
                    etCardNumber.getText().toString().trim());
            params.put(VerifyPhoneActivity.CARD_CODE_S,
                    cardInfo.getBank_code());
            params.put(VerifyPhoneActivity.BANK_NAME_S,
                    cardInfo.getBank_name());
            params.put(VerifyPhoneActivity.CARD_TYPE_I,1);
            ActivitySkipUtil.skipAnotherActivity(CardHolderInfoActivity.this,
                    BankCardInfoActivity.class,params);
            finish();
        }else{
            //信用卡
            ToastUtil.showShort("请绑定本人储蓄卡");
        }
    }
}
